﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BEELab
{
    public partial class Default : System.Web.UI.Page
    {
        /// <summary>
        /// Occures on every page load (first time or post back)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            

        }

        /// <summary>
        /// Occures once an existing user is trying to sign in to the site
        /// </summary>
        /// <returns>True if authentication succeeded, False otherwise</returns>
        private Boolean SignIn()
        {
            Boolean res = BEELab.Logic.UserManagement.AuthenticateUser(txtEmail.Text, txtPassword.Text);
            
            if (res)
            {
                Session["UserID"] = BEELab.Logic.UserManagement.GetUserID(txtEmail.Text);
                Session["UserEmail"] = txtEmail.Text;
                if (BEELab.Logic.UserManagement.IsAdmin(txtEmail.Text))
                {
                    Session["Admin"] = true;
                }
                else
                {
                    Session["Admin"] = false;
                }
            }

            return res;
        }

        /// <summary>
        /// Creates a new user an inserts it to DB
        /// </summary>
        /// <returns>True if succeeded, False otherwise</returns>
        private Boolean SignUp()
        {
            if (EmailRegistered(txtRegEmail.Text))
            {
                return false;
            }

            return -1 != BEELab.Logic.UserManagement.CreateUser(txtFirstName.Text, txtLastName.Text, txtRegEmail.Text, txtUniversity.Text, false, txtRegPassword.Text);
        }

        /// <summary>
        /// Gets an email address, and checks if email already exists on DB
        /// </summary>
        /// <param name="email">email address</param>
        /// <returns>True if email exists, False otherwise</returns>
        private Boolean EmailRegistered(string email)
        {
            return BEELab.Logic.UserManagement.EmailRegistered(email);
        }

        private void PasswordRecovery(string email)
        {
            //TODO Implement
        }

        /// <summary>
        /// Occuers once clicking on sign in button, 
        /// if user exists on DB - will sign him in to main page,
        /// Else - will show an error message
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSignIn_Click(object sender, EventArgs e)
        {
            if (SignIn())
            {
                Response.Redirect("Main.aspx", false);
            }
            else
            {
                lblFailureText.Visible = true;
            }
        }

        /// <summary>
        /// Occuers once clicking on sign up button.
        /// A new user will be created on DB
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// </summary>
        protected void btnSignUp_Click(object sender, EventArgs e)
        {
            if (SignUp())
            {
                txtEmail.Text = txtRegEmail.Text;
                txtPassword.Text = txtRegPassword.Text;
                btnSignIn_Click(this, e);
            }
        }

        /// <summary>
        /// Ocuures onces an already registered user is trying to sign up to the website.
        /// Will show an error on screen
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtRegEmail_TextChanged(object sender, EventArgs e)
        {
            lblEmailVerification.Visible = EmailRegistered(txtRegEmail.Text);
        }
    }
}